<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/15/0015
 * Time: 14:47
 */

namespace app\sports_admin\controller;

use think\Request;
use app\sports_admin\model\Grade as GradeModel;
use app\sports_admin\service\Grade as GradeService;
use app\sports_admin\vaildate\Grade as GradeVaildate;
class Grade extends Base
{
    protected $beforeActionList = [
        'isResource'=>['only'=>'index,gradelist,add,insert,edit,update,delete'],
        'operationSelect'=>['only'=>'index,gradelist,add,edit'],
        'operationInsert'=>['only'=>'insert'],
        'operationDelete'=>['only'=>'delete']
    ];
    /*
     * 年级设置首页
     */
    public function index()
    {
        return view('index');
    }
    /**
     * 获取年级列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     */
    public function gradeList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        return GradeModel::gradeList($page,$rows,$sort,$sortOrder);
    }
    /*
     * 添加年级
     */
    public function add()
    {
        return view('add');
    }
    /*
     * 添加年级信息
     */
    public function insert(Request $request)
    {
        if($request->isAjax()){
            (new GradeVaildate())->insert();
            return GradeService::insert();
        }
    }
    /*
     * 编辑
     * @param $id 年级信息id
     */
    public function edit()
    {
        $id = input('get.id');
        $info = GradeModel::where('id',$id)->find();
        return view('edit',compact('info'));
    }
    /*
     * 修改信息
     */
    public function update(Request $request)
    {
        if($request->isAjax()){
            (new GradeVaildate())->update();
            return GradeService::update();
        }
    }
    /*
     * 删除年级信息
     */
    public function delete(Request $request)
    {
        if($request->isAjax()){
            (new GradeVaildate())->go_check();
            return GradeService::delete();
        }
    }
}